package com.jcark.arch.net

import android.text.TextUtils
import com.jcark.arch.cache.MKV
import me.jessyan.retrofiturlmanager.RetrofitUrlManager
import okhttp3.Interceptor

/**
 * 网络配置管理器
 */
enum class NetConfigManager {
    INSTANCE;

    companion object {
        const val APP_ID = "app_id"
        const val SUCCESS_CODE = "net_config_success_code"
        const val NET_CONFIG_TOKEN = "net_config_token"
        const val NET_CONFIG_SIGN = "net_config_sign"
    }

    private val mInterceptorList: MutableList<Interceptor> = mutableListOf()
    private var mBaseUrl: String = ""
    private var mFlavor: String = ""
    private var mSysUpgrade: String? = "test"
    private var mIsUseSysUpgradeParam = false
    private var mSign = ""

    /**
     * 初始化并重置相关配置信息，应最先调用
     */
    fun init(): NetConfigManager {
        mInterceptorList.clear()
        mBaseUrl = ""
        return this
    }

    /**
     * 设置baseURL
     * @param baseUrl String
     * @return NetConfigManager
     */
    fun setBaseUrl(baseUrl: String): NetConfigManager {
        mBaseUrl = baseUrl
        return this
    }

    fun getBaseUrl(): String {
        return mBaseUrl
    }

    /**
     * 设置APP打包环境
     */
    fun setFlavor(flavor: String): NetConfigManager {
        mFlavor = flavor
        return this
    }

    /**
     * 获取APP打包环境
     */
    fun getFlavor(): String {
        return mFlavor
    }

    /**
     * 设置【SysUpgrade】参数，用于服务端区别是否维护中
     *  product环境不传此参数(值为null时不会传递此参数)
     */
    fun setSysUpgrade(sysUpgrade: String?): NetConfigManager {
        mSysUpgrade = sysUpgrade
        return this
    }

    /**
     * 获取参数【SysUpgrade】值
     *  product环境不传此参数(值为null时不会传递此参数)
     */
    fun getSysUpgrade(): String? {
        return mSysUpgrade
    }

    /**
     * 设置是否添加【sysUpgrade】参数
     */
    fun setIsUseSysUpgradeParam(isUse: Boolean): NetConfigManager {
        mIsUseSysUpgradeParam = isUse
        return this
    }

    /**
     * 是否添加【sysUpgrade】参数
     */
    fun isUseSysUpgradeParam(): Boolean {
        return mIsUseSysUpgradeParam
    }

    /**
     * 增加拦截器
     * @param interceptor 拦截器
     */
    fun addInterceptor(interceptor: Interceptor): NetConfigManager {
        mInterceptorList += interceptor
        return this
    }

    /**
     * 设置App Id
     * @param appId String
     * @return NetConfigManager
     */
    fun setAppId(appId: String): NetConfigManager {
        MKV.put(APP_ID, appId)
        return this
    }

    /**
     * 创建Retrofit Builder,应在设置配置信息后调用
     */
    fun create() {
        //RetrofitUrlManager.getInstance().startAdvancedModel("https://appdev.msttmc.com/mst-app/")
        NetworkApi.INSTANCE.addInterceptors(mInterceptorList)

        NetworkApi.INSTANCE.createRetrofitBuilder(mBaseUrl)
    }

    fun getAppId() = MKV.getString(APP_ID)

    /**
     * 设置请求成功 状态码码
     * @param code Int
     * @return NetConfigManager
     */
    fun setSuccessCode(code: Int): NetConfigManager {
        MKV.put(SUCCESS_CODE, code)
        return this
    }

    fun getSuccessCode() = MKV.getInt(SUCCESS_CODE, 0)

    /**
     * 更换baseURL
     * @param url String
     */
    fun changeBaseUrl(url: String) {
        MKV.put("baseUrl",url)

        RetrofitUrlManager.getInstance().setGlobalDomain(url)
    }

    /**
     *不使用baseUrl ，不能被替换的Url 需要写全地址
     * @param url String
     * @return (String..String?)
     */
    fun setUrlNotChange(url: String) = RetrofitUrlManager.getInstance().setUrlNotChange(url)

    fun setToken(token: String) {
        MKV.put(NET_CONFIG_TOKEN, token)
    }

    fun getToken() = MKV.getString(NET_CONFIG_TOKEN)

    fun setSign(sign: String) {
        mSign = sign
        MKV.put(NET_CONFIG_SIGN, sign)
    }

    fun getSign(): String {
        val sign = MKV.getString(NET_CONFIG_SIGN)
        if (!TextUtils.isEmpty(sign)) {
            return sign
        }
        return mSign
    }

    fun onError(requestErrorCallback: RequestErrorCallback): NetConfigManager {
        NetworkApi.INSTANCE.requestErrorCallback = requestErrorCallback
        return this
    }
}